<template>
  <div :class="warpClass">
    <div class="list" ref="list">
      <div
        v-for="(m, i) in chatRecordList"
        :key="i"
        :class="[itemClass, m.type === 0 ? 'bot' : 'self']"
      >
        {{ m.content }}
      </div>
    </div>
    <div class="bottom">
      <el-input
        placeholder="请输入内容"
        v-model="currentContent"
        @keydown.enter.native="send"
      >
        <el-button slot="append" @click="send">发送</el-button>
      </el-input>
    </div>
  </div>
</template>

<script>
const cls = "custom-service";
import axios from "axios";
export default {
  data() {
    return {
      chatRecordList: [
        {
          type: 0,
          content: `您好，我是人工客服${Math.floor(
            Math.random() * 100
          )}号，很高兴为您服务！`,
        },
      ],
      currentContent: "",
    };
  },

  computed: {
    warpClass() {
      return cls + "-wrap";
    },
    itemClass() {
      return cls + "-item";
    },
  },

  watch: {
    chatRecordList: {
      handler() {
        this.$nextTick(() => {
          this.scrollToBottom();
        });
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    send() {
      this.chatRecordList.push({
        type: 1,
        content: this.currentContent,
      });
      this.currentContent = "";
      const data = {
        key: "e5e78db64c3744fda06533e6729737da",
        info: this.currentContent,
        box: "",
      };
      const config = {
        method: "post",
        url: "/customer",
        headers: {
          "Content-Type": "application/json",
        },
        data: data,
      };
      axios(config)
        .then((res) => {
          const { text } = res.data;
          setTimeout(() => {
            this.chatRecordList.push({
              type: 0,
              content: text,
            });
          }, 1000);
        })
        .catch((err) => {
          setTimeout(() => {
            this.chatRecordList.push({
              type: 0,
              content: "我好像坏掉了，请稍等后重试。",
            });
          }, 1000);
          console.log(err);
        });
    },

    scrollToBottom() {
      if (!this.$refs.list) return;
      const scrollHeight = this.$refs.list.scrollHeight;
      this.$refs.list.scrollTo(0, scrollHeight);
    },
  },

  mounted() {},
};
</script>

<style scoped lang="scss">
.custom-service-wrap {
  padding: 20px;
}

.list {
  height: calc(100vh - 130px);
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  border: 1px solid #dcdfe6;

  &::-webkit-scrollbar {
    width: 5px;
  }

  &::-webkit-scrollbar-track {
    width: 5px;
    border-radius: 10px;
    background: #f0f0f0;
  }

  &::-webkit-scrollbar-thumb {
    width: 5px;
    border-radius: 10px;
    background: #ddd;
  }
}

.custom-service-item {
  display: inline-block;
  padding: 5px;
  border-radius: 4px;
  margin-bottom: 10px;
  max-width: calc(100% - 40px);
  box-shadow: 0 0 4px #00000040;

  &.bot {
    align-self: flex-start;
    background: #f0f0f0;
  }

  &.self {
    align-self: flex-end;
    background: #1890ff;
    color: #fff;
  }
}

.bottom {
  margin-top: 5px;
  height: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;

  input {
    height: 100%;
  }
}
</style>
